<style lang="less">
@import "./own-space.less";
</style>

<template>
    <div class="own-space">

        <Form ref="employeeForm" :model="employee" :rules="employeeRules">
            <Card>
                <p slot="title" class="own-space-title">
                    基本信息
                </p>
                <Row :gutter="8">
                    <Col span="5">
                        <Row>
                            <img :src="headImg" style="width:210px;height:300px;"/>

                            <Upload action="" :before-upload="beforeUpload" class="mt-20 ta-c" v-if="isEditType">
                                <Button type="primary">更换头像</Button>
                            </Upload>
                        </Row>
                    </Col>
                    <Col span="17">
                        <Row class="accountSty1">
                            <span class="accountSty1-span">
                                员工号: {{employee.account}}
                            </span>
                        </Row>
                        <Row 
                            v-for="(row, index) in baseInfoLayout.rows"
                            :key="index" 
                            :gutter="32" 
                            type="flex"
                            justify="start">
                            <Col v-for="(col, index2) in row.cols" :key="index2" :span="6*col.span">
                                <FormItem :label="col.label" :prop="col.key" v-if="!isEditType">
                                    {{getValue(col, employee[col.key])}}
                                </FormItem>
                                <FormItem :label="col.label" :prop="col.key" v-else>
                                    <Input 
                                        v-model="employee[col.key]" 
                                        :placeholder="col.placeholder" 
                                        :disabled="col.key == 'mobile' || col.key == 'email' && employee.hasEmail"
                                        v-if="col.type == 'input'" 
                                        style="width:100%;" 
                                        @on-blur="getEmployee(col.key, employee[col.key])">
                                    </Input>
                                    <DatePicker 
                                        v-model="employee[col.key]" 
                                        type="date" 
                                        :placeholder="col.placeholder"
                                        v-if="col.type == 'datePicker'" 
                                        style="width:100%;"
                                        @on-change="getDatePicker($event, col)">
                                    </DatePicker>
                                    <Select 
                                        v-model="employee[col.key]" 
                                        :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key != 'organizationList' && col.key != 'positionList' && col.key != 'userlevel'"
                                        @on-change="selectChange(employee, col.key, col.selectList)"
                                        style="width:100%;">
                                            <Option 
                                                v-for="(item, index3) in col.selectList"
                                                :key="index3"
                                                :label="item.name"
                                                :value="item.code">
                                            </Option>
                                    </Select>
                                    <Input 
                                        v-model="employee[col.key]" 
                                        :placeholder="col.placeholder" 
                                        type="textarea"
                                        :rows="4"
                                        v-if="col.type == 'textarea'" 
                                        style="width:100%;">
                                    </Input>
                                    <Select 
                                        v-model="departmentSelectList" 
                                        multiple 
                                        :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key == 'organizationList'" style="width:100%;">
                                            <Option 
                                                v-for="(item, index3) in organizationList"
                                                :key="index3"
                                                :label="item.organizationName"
                                                :value="item.id">
                                            </Option>
                                    </Select>
                                    <Select v-model="employee[col.key]"  :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key == 'userlevel'"
                                        @on-change="selectChange2(employee, col.key, userlevelList)"
                                        style="width:100%;">
                                    <Option v-for="(item, index) in userlevelList" :label="item.levelName"
                                            :key="index"
                                            :value="item.id"></Option>
                                    </Select>
                                    <Select 
                                        v-model="positionSelectList" 
                                        :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key == 'positionList'" 
                                        multiple 
                                        style="width:100%;">
                                            <OptionGroup 
                                                v-for="(positionGroup, positionGroupindex) in positionGroupList"
                                                :key="positionGroupindex"
                                                :label="positionGroup.organizationName">
                                                    <Option 
                                                        v-for="(item, index3) in positionGroup.positionList"
                                                        :key="index3"
                                                        :value="item.id" 
                                                        :label="item.name">
                                                    </Option>
                                            </OptionGroup>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                    </Col>
                    <Col span="4">
                        <div>
                            <Button type="primary" @click="editInfo" v-if="!isEditType">
                                修改信息
                            </Button>
                            <div v-else>
                                <Button type="warning" @click="cancelEditInfo">
                                    取消
                                </Button>
                                <Button type="warning" @click="submit">
                                    提交审核
                                </Button>
                            </div>
                            
                        </div>
                    </Col>
                </Row>
            </Card>
            <Card>
                <p slot="title" class="own-space-title">
                    其他信息
                </p>
                <Row>
                    <Row v-for="(row, index) in otherInfoLayout.rows" :key="index" :gutter="32" type="flex" justify="start">
                        <Col v-for="(col, index2) in row.cols" :key="index2" :span="6*col.span">
                            <FormItem :label="col.label" :prop="col.key" v-if="!isEditType">
                                {{getValue(col, employee[col.key])}}
                            </FormItem>
                            <FormItem :label="col.label" :prop="col.key" v-else>
                                <Input v-model="employee[col.key]" :placeholder="col.placeholder" :disabled="col.key == 'mobile' || col.key == 'email' && employee.hasEmail"
                                    v-if="col.type == 'input'" style="width:100%;" @on-blur="getEmployee(col.key, employee[col.key])"></Input>
                                <DatePicker v-model="employee[col.key]" type="date" :placeholder="col.placeholder"
                                            v-if="col.type == 'datePicker'" style="width:100%;" @on-change="getDatePicker"></DatePicker>
                               <Select v-model="employee[col.key]" :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key != 'organizationList' && col.key != 'positionList'"
                                        @on-change="selectChange(employee, col.key, col.selectList)"
                                        style="width:100%;">
                                    <Option v-for="(item, index) in col.selectList" :label="item.name"
                                            :key="index"
                                            :value="item.code"></Option>
                                </Select>
                                <Input v-model="employee[col.key]" :placeholder="col.placeholder" type="textarea"
                                    :rows="4"
                                    v-if="col.type == 'textarea'" style="width:100%;"></Input>
                                <Select v-model="departmentSelectList" multiple :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key == 'organizationList'" style="width:100%;">
                                    <Option v-for="(item, index3) in organizationList" :label="item.organizationName"
                                            :key="index3"
                                            :value="item.id"></Option>
                                </Select>
                                <Select v-model="positionSelectList" :placeholder="col.placeholder"
                                        v-if="col.type == 'select' && col.key == 'positionList'" multiple style="width:100%;">
                                    <OptionGroup :key="positionGroupindex" v-for="(positionGroup, positionGroupindex) in positionGroupList" :label="positionGroup.organizationName">
                                        <Option :key="index3" v-for="(item, index3) in positionGroup.positionList" :value="item.id" :label="item.name"></Option>
                                    </OptionGroup>
                                </Select>
                            </FormItem>
                        </Col>
                    </Row>
                </Row>
            </Card>
            <Card>
                <p slot="title" class="own-space-title">
                    证件信息
                </p>
                <Row :gutter="32">
                    <Col span="24">
                        <div class="upLoadBox">
                            <div v-if="employee.userCertificateList.length > 0" class="upLoadBoxList">
                                <div v-for="(item, index) in employee.userCertificateList" :key="index" class="upLoadBoxItem" :class="{type2:isEditType}">
                                    <div class="upLoadBoxItem-imgBox">
                                        <img :src="item.documentUrl" />
                                    </div>
                                    <div class="ta-c mt-10">{{item.name}}</div>
                                    <div class="btnBox" v-if="isEditType">
                                        <div>
                                            <Button @click="openPaperMod(index, item.name)">
                                                更换证件照
                                            </Button>
                                        </div>
                                        <div class="mt-20">
                                            <Button @click="delCurrentImg(index)">
                                                删除
                                            </Button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="upLoadBoxItem">
                                <img v-if="isEditType" @click="openPaperMod('isNew')" :src="require('@/images/pic06.png')" style="width:140px;"/>
                                <div v-if="employee.userCertificateList.length == 0" class="ta-c mt-10">暂无证件信息</div>
                            </div>
                        </div>
                    </Col>
                </Row>
            </Card>
        </Form>
        <Modal
            v-model="paperMod"
            title="上传证件照"
            :mask-closable="false"
            :draggable="true">
            
            <Form ref="paperForm" :model="userCertificateListItem" :rules="paperFormRule" :label-width="80">
                <FormItem label="证件名称:" prop="name">
                    <Input v-model="userCertificateListItem.name" placeholder="请输入证件名称"></Input>
                </FormItem>
                <FormItem label="上传证件:" prop="documentUrl">
                    <img v-if="userCertificateListItem.documentUrl" :src="userCertificateListItem.documentUrl" style="width:210px;"/>
                    <Upload v-else action="" :before-upload="beforeUploadImg">
                        <img :src="require('@/images/pic05.png')" style="width:210px;"/>
                    </Upload>
                </FormItem>
                
            </Form>
            
            <div slot="footer">
                <Button :loading="paperModLoading" @click="cancelPaper('paperForm')">取消</Button>
                <Button type="primary" :loading="paperModLoading" @click="subPaper('paperForm')">上传</Button>
            </div>
        </Modal>
        <Spin size="large" fix v-show="showSpin">
            {{spinMsg}}
        </Spin>
    </div>
</template>

<script>
import moment from 'moment';
export default {
    name: 'ownspace_index',
    data () {
        return {
            layoutData: {},
            employee: {
                organizationList: [],
                position: [],
                userCertificateList: []
            },
            labelMap: {},
            employeeRules: {},
            organizationList: [],
            positionGroupList: [],
            positionList: [],
            departmentSelectList: [],
            positionSelectList: [],
            createNext: false,
            optType: 'ADD',
            reg: {
                bank: /^[a-zA-Z0-9()\u4e00-\u9fa5]+$/,
                cardSn: /^[0-9]{0,20}$/,
                mobile: /^[1][0-9]{10}$/,
                cardNo: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
                email: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
            },
            showSpin: false,
            spinMsg: '加载中，请稍后...',
            userlevelList: [],
            baseInfoLayout: {},
            otherInfoLayout: {},
            certificatesInfoLayout: {},
            isEditType: false,
            headImg: require('@/images/pic04.png'),
            paperImg: require('@/images/pic04.png'),
            paperMod: false,
            paperModLoading: false,
            userCertificateListItem: {
                name: '',
                documentUrl: '',
                documentId: ''
            },
            paperFormRule: {
                name: [
                    { required: true, message: '请输入证件名称', type: 'string', trigger: 'blur' }
                ],
                documentUrl: [
                    { required: true, message: '请上传证件', type: 'string', trigger: 'blur' } // change
                ]
            },
            currentIndex: null
        };
    },
    mounted () {
        this.loadLayoutData();
        // 加载下拉框选项
        this.loadSelectOptionsData();
        this.loadUserLevels();
    },
    methods: {
        // 关闭当前页面
        closePage () {
            this.$store.commit('removeTag', 'ownspace_index');
            this.$store.commit('closePage', 'ownspace_index');
            this.$router.push('/home');
        },
        // 加载布局信息
        loadLayoutData () {
            this.showSpin = true;
            depot.get({
                url: this.URL.DATA.FIND_FORM_LAYOUT_DATA,
                config: {
                    params: {
                        type: 'formLayout'
                    }
                },
                cb: (data) => {
                    this.showSpin = false;
                    if (data.code === 0) {
                        if (!data.data || !data.data[0].context) {
                            this.$Message.error('未找到布局信息！');
                            this.closePage();
                            return;
                        }
                        let context = data.data[0].context;
                        let formData = JSON.parse(context);
                        if (!formData || !formData.layout) {
                            this.$Message.error('未找到布局信息！');
                            this.closePage();
                            return;
                        }
                        this.layoutData = formData.layout;

                        let validateRules = formData.validateRules;
                        console.log(validateRules, '规则是啥');
                        // 加工产生校验规则
                        if (validateRules && validateRules.length > 0 && validateRules instanceof Array) {
                            validateRules.forEach((validateRule) => {
                                if (validateRule.rules && validateRule.rules instanceof Array) {
                                    validateRule.rules.forEach((rule) => {
                                        if (rule && rule.hasOwnProperty('validator')) {
                                            rule.validator = this[rule.validator];
                                        }
                                    });
                                }
                                this.employeeRules[validateRule.key] = validateRule.rules;
                            });
                        }
                        console.log(this.employeeRules, '绑定的规则是');
                        if (this.layoutData && this.layoutData.cards.length) {
                            this.layoutData.cards.forEach((card) => {
                                if (card) {
                                    if (card.type === 'baseInfo') {
                                        this.baseInfoLayout = card;
                                    } else if (card.type === 'otherInfo') {
                                        this.otherInfoLayout = card;
                                    } else if (card.type === 'certificatesInfo') {
                                        this.certificatesInfoLayout = card;
                                    }
                                }
                            });
                            this.loadEmployeeData();
                        }
                    } else if (data.code === 999) {
                        this.$Message.error(data.message);
                    }
                },
                ce: (error) => {
                    this.showSpin = false;
                }
            });
        },
        // 加载员工等级数据
        loadUserLevels () {
            depot.post({
                url: this.URL.USER.FIND_USER_LEVEL,
                data: {},
                cb: (data) => {
                    this.showSpin = false;
                    if (data.code === 0) {
                        this.userlevelList = data.data;
                        console.log(this.userlevelList);
                    } else if (data.code === 999) {
                        this.$Message.error(data.message);
                    }
                },
                ce: (error) => {
                }
            });
        },
        // 加载下拉框选项
        loadSelectOptionsData () {
            // 获取部门列表数据
            this.showSpin = true;
            depot.get({
                url: this.URL.USER.FIND_ORGANIZATION_LIST,
                cb: (data) => {
                    this.showSpin = false;
                    if (data.code === 0) {
                        this.organizationList = data.data;
                    } else if (data.code === 999) {
                        this.$Message.error(data.message);
                    }
                },
                ce: (error) => {
                    this.showSpin = false;
                }
            });
        },
        // 获取用户信息
        loadEmployeeData () {
            this.showSpin = true;
            depot.get({
                url: this.URL.USER.FIND_MY_ALL_INFO,
                cb: (data) => {
                    this.showSpin = false;
                    if (data.code === 0) {
                        this.employee = this.transferEmployee(data.data);
                        this.headImg = this.employee.headPicUrl;
                        // 将organizationList和positionList转义
                        if (this.employee) {
                            if (this.employee.organizationList) {
                                // 保存当前公司的节点
                                let currentCidOrg = [];
                                this.employee.organizationList.forEach((org) => {
                                    currentCidOrg.push(org);
                                    this.departmentSelectList.push(org.id);
                                });
                                this.employee.organizationList = currentCidOrg;
                            }
                            if (this.employee.organizationNodeList) {
                                // 保存当前公司的节点
                                let currentCidNode = [];
                                this.employee.organizationNodeList.forEach((node) => {
                                    currentCidNode.push(node);
                                    this.positionSelectList.push(node.positionId);
                                });
                                this.employee.organizationNodeList = currentCidNode;
                            }
                            if (this.employee.userInfo) {
                                this.employee.userInfo.forEach(info => {
                                    if (info.labelKey === 'userlevel') {
                                        this.employee['userlevel'] = +info.labelValue;
                                    }
                                });
                            }
                        }
                        this.employee.userRelationList = null;
                    } else if (data.code === 999) {
                        this.$Message.error(data.message);
                    }
                },
                ce: (error) => {
                    this.showSpin = false;
                }
            });
        },
        // 将传入数据转义
        transferEmployee (data) {
            let employee = data.user;
            let cid;
            let company = localStorage.getItem('company');
            if (company) {
                cid = JSON.parse(company).id;
            }
            if (!cid) {
                this.$Message.error('获取公司信息失败');
            }
            // 将非本公司的去除
            for (var i = 0; i <= employee.organizationList.length; i++) {
                var organization = employee.organizationList[i];
                if (organization && organization.cid != cid) {
                    employee.organizationList.splice(i, 1);
                }
            }
            for (var i = 0; i <= employee.organizationNodeList.length; i++) {
                var organizationNode = employee.organizationNodeList[i];
                if (organizationNode && organizationNode.cid != cid) {
                    employee.organizationNodeList.splice(i, 1);
                }
            }
            for (var i = 0; i <= employee.positionList.length; i++) {
                var position = employee.positionList[i];
                if (position && position.cid != cid) {
                    employee.positionList.splice(i, 1);
                }
            }
            employee.name = data.name;
            employee.headPicId = data.headPicId;
            employee.headPicUrl = data.headPicUrl;
            employee.idCardNo = data.idCardNo;
            employee.userInfo = data.userInfo;
            employee.remark = data.remark;
            if (employee.userInfo) {
                employee.userInfo.forEach((info) => {
                    if (info.labelKey === 'userlevel') {
                        employee.userlevel = info.labelName;
                    } else {
                        employee[info.labelKey] = info.labelValue;
                    }
                });
            }
            // 如果存在邮箱
            if (employee.email) {
                employee.hasEmail = true;
            } else {
                employee.hasEmail = false;
            }
            return employee;
        },
        // 修改选择项
        selectChange (data, key, selectList) {
            if (selectList && data && key && data[key]) {
                if (selectList.length > 0) {
                    // 查找选中值，设置Label
                    selectList.forEach((item) => {
                        if (item['code'] == data[key]) {
                            this.labelMap[key] = item['name'];
                        }
                    });
                }
            } else {
                console.error('选择下拉框选项时报错');
            }
        },
        // 修改选择项
        selectChange2 (data, key, selectList) {
            // console.log(selectList, data, key, data[key], 1111);
            if (selectList && data && key && data[key]) {
                if (selectList.length > 0) {
                    // 查找选中值，设置Label
                    selectList.forEach((item) => {
                        if (item['id'] == data[key]) {
                            this.labelMap[key] = item['levelName'];
                        }
                    });
                }
            } else {
                console.error('选择下拉框选项时报错');
            }
            console.log(this.labelMap, '看看');
        },
        // 校验手机号
        validateMobile (rule, value, callback) {
            if (value && !this.reg.mobile.test(value)) {
                if (callback) {
                    callback(new Error('手机号格式填写有误'));
                }
                return false;
            } else {
                if (callback) {
                    callback();
                }
                return true;
            }
        },
        // 校验卡号
        validateSn (rule, value, callback) {
            if (value && !this.reg.cardSn.test(value)) {
                if (callback) {
                    callback(new Error('银行卡号格式输入错误'));
                }
                return false;
            } else {
                if (callback) {
                    callback();
                }
                return true;
            }
        },
        // 校验银行名称
        validateBank (rule, value, callback) {
            if (value && !this.reg.bank.test(value)) {
                if (callback) {
                    callback(new Error('银行格式输入错误'));
                }
                return false;
            } else {
                if (callback) {
                    callback();
                }
                return true;
            }
        },
        // 校验身份证号
        validateCardNo (rule, value, callback) {
            if (value && !this.reg.cardNo.test(value)) {
                if (callback) {
                    callback(new Error('身份证号格式填写有误'));
                }
                return false;
            } else {
                if (callback) {
                    callback();
                }
                return true;
            }
        },
        // 校验邮箱
        validateEmail (rule, value, callback) {
            if (value && !this.reg.email.test(value)) {
                if (callback) {
                    callback(new Error('邮箱格式填写有误'));
                }
                return false;
            } else {
                if (callback) {
                    callback();
                }
                return true;
            }
        },
        getEmployee (key, value) {
            if (key === 'idCardNo' && value.length == 18) {
                let year = value.substring(6, 10);
                let month = value.substring(10, 12);
                let day = value.substring(12, 14);
                this.employee['birthday'] = year + '-' + month + '-' + day;
                this.$forceUpdate();
            } else {
                // 测试提BUG只有身份证带出 其他选项不能消除出生日期
                // this.employee['birthday'] = '';
            }
            // TODO 依据用户姓名和身份证号从其他公司或已删除的数据中，查找用户信息,并自动填充
        },
        getDatePicker (data, val2) {
            if (this.employee['idCardNo'] && val2.key === 'birthday') {
                let waitObj = '';
                let year = this.employee['idCardNo'].substring(6, 10);
                let month = this.employee['idCardNo'].substring(10, 12);
                let day = this.employee['idCardNo'].substring(12, 14);
                waitObj = year + '-' + month + '-' + day;
                if (waitObj !== data) {
                    this.$Message.error({
                        content: '出生日期与身份证日期不一致',
                        duration: 5
                    });
                }
            }
        },

        // 提交
        submit () {
            this.$refs['employeeForm'].validate((valid) => {
                if (valid) {
                    var submitData = {};
                    let user = this.getProps();
                    user.organizationList = null;
                    user.positionList = null;

                    let propsUser = JSON.parse(JSON.stringify(user));
                    submitData.props = propsUser;

                    submitData.user = user;
                    submitData.user.userCertificateList = this.employee.userCertificateList;

                    submitData.labelMap = this.labelMap;
                    submitData.organizationPositionList = this.getOrgPosList();
                    this.showSpin = true;
                    this.spinMessage = '提交中,请稍后...';
                    if (submitData.organizationPositionList && submitData.organizationPositionList.length > 0) {
                        depot.get({
                            url: this.URL.USER.IS_DEPARTMENT,
                            config: {
                                params: {
                                    array: JSON.stringify(submitData.organizationPositionList)
                                }
                            },
                            cb: (data) => {
                                this.showSpin = false;
                                if (data.code === 0) {
                                    if (data.data && data.data.length > 0) {
                                        let dempartmentStr = '';
                                        if (data.data instanceof Array) {
                                            data.data.forEach((dempartment) => {
                                                dempartmentStr += dempartment.organization + ',';
                                            });
                                            dempartmentStr = dempartmentStr.substr(0, dempartmentStr.length - 1);
                                        }
                                        this.$Modal.confirm({
                                            title: '确认',
                                            content: '您选择的部门：' + dempartmentStr + ' 的负责人将变更为' + this.employee.name + ', 是否确认？',
                                            onOk: () => {
                                                this.showSpin = true;
                                                this.spinMsg = '提交中，请稍后...';
                                                this.submitEmployee(submitData);
                                            },
                                            onCancel: () => {

                                            }
                                        });
                                    } else {
                                        this.submitEmployee(submitData);
                                    }
                                } else {
                                    this.$Message.error(data.message);
                                }
                            }
                        });
                    } else {
                        this.submitEmployee(submitData);
                    }
                } else {
                    this.$Message.error('您输入的信息有误，请检查');
                }
            });
        },
        submitEmployee (submitData) {
            depot.post({
                url: this.URL.USER.SAVE_USER,
                data: submitData,
                cb: (data) => {
                    this.showSpin = false;
                    if (data.code === 0) {
                        this.$Message.success('提交成功');
                        this.isEditType = false;
                        if (this.$route.params.refreshMethod) {
                            this.$route.params.refreshMethod();
                        }
                        if (!this.createNext) {
                            this.closePage();
                        }
                    } else {
                        this.$Message.error(data.message);
                    }
                },
                ce: (error) => {
                    this.showSpin = false;
                }
            });
        },
        // 只提交表单信息
        getProps () {
            let user = {
                id: this.employee.id
            };
            if (this.employeeRules) {
                for (var employeeKey in this.employeeRules) {
                    if (this.employee[employeeKey]) {
                        user[employeeKey] = this.employee[employeeKey];
                        if (this.employee[employeeKey] instanceof Date) {
                            user[employeeKey] = moment(this.employee[employeeKey]).format('YYYY-MM-DD');
                        }
                    }
                }
            }
            return user;
        },
        // 获取组织和职位数据
        getOrgPosList () {
            let orgPosList = [];
            if (this.positionSelectList) {
                this.positionSelectList.forEach((selectPosition) => {
                    let selectedPosFound = false;
                    if (this.positionGroupList) {
                        this.positionGroupList.forEach((positionGroup) => {
                            if (positionGroup.positionList) {
                                positionGroup.positionList.forEach((position) => {
                                    if (selectPosition == position.id) {
                                        selectedPosFound = true;
                                        orgPosList.push({
                                            organizationId: positionGroup.id,
                                            positionId: selectPosition
                                        });
                                    }
                                });
                            }
                            if (selectedPosFound == true) {

                            }
                        });
                    }
                });
            }

            // 查找未设置职位的部门
            if (this.departmentSelectList) {
                let noPosOrganizationList = [];
                this.departmentSelectList.forEach((org) => {
                    let orgFound = false;
                    if (orgPosList) {
                        orgPosList.forEach((orgPos) => {
                            if (orgPos.organizationId == org) {
                                orgFound = true;
                            }
                        });
                        // 未查询到部门，则加到部门列表中
                        if (!orgFound) {
                            noPosOrganizationList.push({
                                organizationId: org,
                                positionId: null
                            });
                        }
                    }
                });
                orgPosList = orgPosList.concat(noPosOrganizationList);
            }
            return orgPosList;
        },
        // 页面显示值
        getValue (col, value) {
            let newValue = value;
            if (col && col.type) {
                if (col.type == 'select') {
                    if (col.key == 'organizationList') {
                        if (this.employee) {
                            if (this.employee.organizationList) {
                                newValue = '';
                                if (this.employee.organizationList instanceof Array) {
                                    this.employee.organizationList.forEach((org) => {
                                        if (org.organizationName) {
                                            newValue += org.organizationName + ',';
                                        }
                                    });
                                }
                                if (newValue && newValue.length > 1) {
                                    newValue = newValue.substr(0, newValue.length - 1);
                                }
                            }
                        }
                    } else if (col.key == 'positionList') {
                        if (this.employee) {
                            if (this.employee.organizationNodeList) {
                                newValue = '';
                                if (this.employee.organizationNodeList instanceof Array) {
                                    this.employee.organizationNodeList.forEach((pos) => {
                                        if (pos.position) {
                                            newValue += pos.position + ',';
                                        }
                                    });
                                }
                                if (newValue && newValue.length > 1) {
                                    newValue = newValue.substr(0, newValue.length - 1);
                                }
                            }
                        }
                    } else {
                        if (col.selectList) {
                            col.selectList.forEach((selectData) => {
                                if (selectData && selectData.code && selectData.code == value) {
                                    newValue = selectData.name;
                                }
                            });
                        } else if (col.key === 'userlevel') {
                            if (this.employee.userInfo instanceof Array) {
                                this.employee.userInfo.forEach(info => {
                                    if (info.labelKey === col.key) {
                                        newValue = info.labelName;
                                    }
                                });
                            }
                        }
                    }
                } else if (col.type == 'datePicker') {
                    // console.log(newValue, 123123);
                    var d = new Date(newValue);
                    newValue = d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate() < 10 ? '0' + d.getDate() : d.getDate());
                    //  + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
                }
            }
            if (!newValue) {
                newValue = '无';
            }
            return newValue;
        },
        editInfo () {
            this.isEditType = true;
            // 编辑时模拟员工等级选中事件
            this.selectChange2(this.employee, 'userlevel', this.userlevelList);
        },
        cancelEditInfo () {
            this.isEditType = false;
        },
        // 上传头像之前的方法
        beforeUpload (file) {
            let _this = this;
            _this.showSpin = true;
            if (file) {
                let reader = new FileReader();
                reader.onloadend = function () {
                    /* this从vue实例变为reader */
                    let submitData = new FormData();
                    submitData.append('file', file);
                    depot.post({
                        url: _this.URL.DOCUMENT.UPLOAD_DOCUMENT,
                        data: submitData,
                        cb: (data) => {
                            _this.showSpin = false;
                            if (data.code === 0) {
                                _this.headImg = data.data.accessUrl;

                                let submitData = {};
                                submitData.headPicId = data.data.id;
                                submitData.headPicUrl = data.data.accessUrl;

                                depot.post({
                                    url: _this.URL.USER.UPDATE_LOAD_HEAD_PIC,
                                    data: submitData,
                                    cb: (data) => {
                                        if (data.code === 0) {
                                            this.$Message.success('上传头像成功');
                                        } else if (data.code === 999) {
                                            _this.$Message.error(data.message);
                                        }
                                    },
                                    ce: (error) => {

                                    }
                                });
                            } else {
                                _this.$Message.error(data.message);
                            }
                        },
                        ce: (error) => {
                            _this.showSpin = false;
                        }
                    });
                };
                if (file) {
                    reader.readAsDataURL(file);
                }
            }
            return false;
        },
        // 打开上传MOD
        openPaperMod (index, name) {
            if (index === 'isNew') {
                this.currentIndex = null;
                this.userCertificateListItem.name = '';
            } else {
                this.currentIndex = index;
                this.userCertificateListItem.name = name;
            }

            this.paperMod = true;
            this.paperModLoading = false;
        },
        // 上传文件之前的方法
        beforeUploadImg (file) {
            let _this = this;
            _this.showSpin = true;
            if (file) {
                let reader = new FileReader();
                reader.onloadend = function () {
                    /* this从vue实例变为reader */
                    let submitData = new FormData();
                    submitData.append('file', file);
                    depot.post({
                        url: _this.URL.DOCUMENT.UPLOAD_DOCUMENT,
                        data: submitData,
                        cb: (data) => {
                            _this.showSpin = false;
                            if (data.code === 0) {
                                if (!this.currentIndex) {
                                    _this.userCertificateListItem.documentUrl = '';
                                    _this.userCertificateListItem.documentId = '';
                                }
                                _this.userCertificateListItem.documentUrl = data.data.accessUrl;
                                _this.userCertificateListItem.documentId = data.data.id;
                            } else {
                                _this.$Message.error(data.message);
                            }
                        },
                        ce: (error) => {
                            _this.showSpin = false;
                        }
                    });
                };
                if (file) {
                    reader.readAsDataURL(file);
                }
            }
            return false;
        },

        // 上传窗口取消
        cancelPaper (name) {
            this.paperMod = false;
            this.$refs[name].resetFields();
        },
        // 上传窗口上传确定
        subPaper (name) {
            this.paperModLoading = true;
            this.$refs[name].validate((valid) => {
                if (valid) {
                    let flag = false;

                    if (this.currentIndex !== null) {
                        if (this.employee.userCertificateList.length > 0) {
                            this.employee.userCertificateList.forEach((item, index) => {
                                if (this.currentIndex === index) {
                                    item.documentUrl = this.userCertificateListItem.documentUrl;
                                    item.documentIdz = this.userCertificateListItem.documentId;
                                    item.name = this.userCertificateListItem.name;
                                    flag = true;
                                }
                            });
                        }
                    } else {
                        let obj = {};
                        obj.documentUrl = this.userCertificateListItem.documentUrl;
                        obj.documentId = this.userCertificateListItem.documentId;
                        obj.name = this.userCertificateListItem.name;
                        this.employee.userCertificateList.push(obj);
                        flag = true;
                    }
                    if (flag) {
                        this.paperMod = false;
                        this.$refs[name].resetFields();
                        this.$Message.success('上传成功');
                    }
                } else {
                    this.paperModLoading = false;
                    this.$Message.error('验证未通过');
                }
            });
        },
        // 删除当前照片
        delCurrentImg (index) {
            this.$Modal.confirm({
                title: '确认提示',
                content: '确认删除?',
                loading: true,
                onOk: () => {
                    this.$Message.success('删除成功');
                    this.$Modal.remove();
                    this.employee.userCertificateList.splice(index, 1);
                },
                onCancel: () => {
                    this.$Message.info('取消删除');
                }
            });
        }
    },
    watch: {
        'departmentSelectList' () {
            this.positionGroupList = [];
            if (this.departmentSelectList) {
                this.departmentSelectList.forEach((departmentId) => {
                    this.organizationList.forEach((org) => {
                        if (departmentId == org.id) {
                            this.positionGroupList.push(org);
                        }
                    });
                });
            }
        }
    }
};
</script>
